/*******************************************************************************
 * Developer: Ian Doyle
 * Date:      6/23/2012
 *
 * All code Copyright (C) 2012 Ian Doyle. All rights reserved.
 *
 * This file is a part of FileFocus.
 *
 * FileFocus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * FileFocus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with FileFocus.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/


package filefocus;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;


public class FileComparisonSet extends HashSet<File> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -8983368018192246304L;
	
	FilenameFilter filter;
	
	public FileComparisonSet(FilenameFilter fileFilter) throws IOException
	{
		filter = fileFilter;
	}
	
	public FileComparisonSet(Iterable<String> filePaths, FilenameFilter fileFilter) throws IOException
	{
		filter = fileFilter;
		addFilePaths(filePaths);
	}
	
	public void addFilePaths(Iterable<String> filePaths) throws IOException
	{
		Iterator<String> i = filePaths.iterator();
		
		while (i.hasNext())
		{
			addFilePath(i.next());
		}
	}
	
	public void addFilePath(String filePath) throws IOException
	{
		File file = new File(filePath);
		if(file.isFile())
		{
			if(filter.accept(file.getParentFile(), file.getName()))
				add(file.getCanonicalFile());
		}
		else if(file.isDirectory())
		{
			addFilePaths(Arrays.asList(file.list(filter)));
		}
	}
}
